<?php

$db = new models($host,$user,$pass,$db_name);

if(isset($_POST['request_edit'])){
	$id = trim($_POST['qid']);show($id);
	$params = array(
		'tid'	=> 1,//trim($_POST['question_type']),
		'name'	=> trim($_POST['question_name']),
		'level'	=> trim($_POST['question_level']),
		'sid'	=> trim($_POST['question_service'])
	);
	
	if($params['tid'] && $params['name'] && $params['level'] && $params['sid']){
		$db->where("id='$id'");
		$question = $db->get_data("question");
		if($question){
			$db->update("question",$params);
			redirect("$home_url/admin/questions/?notice=update_success");
		}else{
			$error = "Câu hỏi không tồn tại.";
		}
		
	}else{
		$error = "Không được bỏ trống dữ liệu cập nhật.";
	}
}else{
	$id = trim($_GET['id']);
	//Get list services
	$list_services = $db->get_data("services");
	
	$db->reset_data();
	//Get list level
	$list_levels = $db->get_data("level");
	
	$db->reset_data();
	$db->where("id='$id'");
	$question = $db->get_data("question");
	
	if(!$question) redirect("$home_url/admin/questions/?notice=update_error");
}
?>

<div class="container" id="page-question">
	<div class="row">
		<div class="col-sm-12 col-md-12 col-lg-12 text-center">
			<form role="form" class="form-horizontal" method="post" action="<?php echo $home_url; ?>/admin/questions/edit/">
				<input type="hidden" name="qid" value="<?php echo $id;?>" />
				<fieldset class="margin20">
					<legend class="text-center padding10-bottom">CHỈNH SỬA CÂU HỎI</legend>
					<?php
						if($error != ''){
							showAlert("alert-danger alert-error",$error);
						}
					?>
					<!--
					<div class="form-group">
						<label for="question_type" class="control-label col-xs-3">Loại câu hỏi</label>
						<div class="col-xs-6">
							<select name="question_type" id="question_type" class="form-control">
								<option value="1" <?php if($question[0]['id'] == 1) echo "selected";?>>Text</option>
								<option value="2" <?php if($question[0]['id'] == 2) echo "selected";?>>Hình</option>
							</select>
						</div>
					</div> -->
					<div class="form-group">
						<label for="question_name" class="control-label col-xs-3">Câu hỏi</label>
						<div class="col-xs-6">
							<input type="text" name="question_name" id="question_name" class="form-control" size="40" value="<?php echo $question[0]['name'];?>" placeholder="Nhập câu hỏi..." />
						</div>
					</div>
					<div class="form-group">
						<label for="question_level" class="control-label col-xs-3">Cấp độ</label>
						<div class="col-xs-6">
							<select name="question_level" id="question_level" class="form-control">
							<?php
								if($list_levels != null){
									foreach($list_levels as $level){
										if($question[0]['level'] == $level['id'])
											echo "<option value='".$level['id']."' selected>".$level['name']."</option>";
										else
											echo "<option value='".$level['id']."'>".$level['name']."</option>";
									}
								}else{
									echo "<option value=-1>Chưa có dữ liệu</option>";
								}
							?>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="question_service" class="control-label col-xs-3">Thuộc dịch vụ</label>
						<div class="col-xs-6">
							<select name="question_service" id="question_service" class="form-control">
							<?php
								if($list_services != null){
									foreach($list_services as $service){
										if($question[0]['sid'] == $service['id'])
											echo "<option value='".$service['id']."' selected>".$service['name']."</option>";
										else
											echo "<option value='".$service['id']."'>".$service['name']."</option>";
									}
								}else{
									echo "<option value=-1>Chưa có dịch vụ</option>";
								}
							?>
							</select>
						</div>
					</div>
					<div class="form-group">
						<div class="col-xs-offset-3 col-xs-6">
							<button type="submit" name="request_edit" id="request_edit" class="btn btn-primary">Cập nhật</button>
						</div>
					</div>
				</fieldset>
			</form>
		</div>
	</div>
</div>